package automation {
import util.SeleFxCodecHelper;
import util.SeleFxCodecHelper;
import flash.events.Event;
import flash.events.FocusEvent;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.system.ApplicationDomain;
import flash.utils.describeType;

import mx.automation.Automation;
import mx.automation.events.AutomationReplayEvent;
import mx.automation.IAutomationObject;
import mx.automation.IAutomationEventDescriptor;
import mx.core.mx_internal;

use namespace mx_internal;
public class SeleFxEventDescriptor implements IAutomationEventDescriptor {

    private var _name:String;
    private var _eventClassName:String;
    private var _eventType:String;
    private var _args:Array;

    public function SeleFxEventDescriptor(name:String, eventClassName:String, eventType:String, args:Array) {
        super();

        _name = name;
        _eventClassName = eventClassName;
        _eventType = eventType;
        _args = args;
    }

    public function get name():String {
        return _name;
    }

    public function get eventClassName():String {
        return _eventClassName;
    }

    public function get eventType():String {
        return _eventType;
    }

    public function getArgDescriptors(obj:IAutomationObject):Array {
        return _args;
    }

    public function record(target:IAutomationObject, event:Event):Array {
        var args:Array = getArgDescriptors(target);

        var helper:SeleFxCodecHelper = SeleFxCodecHelper.getCodecHelper();
        return helper.encodeProperties(event, args, target);
    }

    public function replay(target:IAutomationObject, args:Array):Object {
        var event:Event = createReplayEvent(target);
        var argDescriptors:Array = getArgDescriptors(target);
        var helper:SeleFxCodecHelper = SeleFxCodecHelper.getCodecHelper();
        helper.decodeProperties(event, args, argDescriptors,
                IAutomationObject(target));

        var riEvent:AutomationReplayEvent = new AutomationReplayEvent();
        riEvent.automationObject = target;
        riEvent.replayableEvent = event;
        Automation.automationManager.replayAutomatableEvent(riEvent);

        return null;
    }

 

    public function createReplayEvent(obj:Object):Event {
        var eventClass:Class =
                SeleFxAutomation.getDefinitionFromObjectDomain(obj, _eventClassName);

        return (eventClass == KeyboardEvent
                ? new KeyboardEvent(KeyboardEvent.KEY_DOWN)
                : (eventClass == FocusEvent &&
                   _eventType == FocusEvent.KEY_FOCUS_CHANGE
            // this event is not like the other children.  it needs
            // to be cancelable.  this should be generalized at some point
            // because other children may need this attention too.
                ? new eventClass(_eventType, true, true)
                : new eventClass(_eventType)));
    }

}
}